package com.sky.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.sky.properties.AliPayProperties;
import com.sky.service.AlipayService;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AliPayProperties config;
    @Autowired
    private AliPayProperties aliPayProperties;


    @Override
    public String createTrade(String zhifubaoorderNumber, String orderAmount) {
        log.info("生成支付宝支付订单");

        // 生成订单信息
        String orderNumber = zhifubaoorderNumber; // 订单号
        String totalAmount = orderAmount; // 金额
        String subject = "支付测试"; // 商品名称

        String app_id = config.getApp_id();
        String private_key = config.getApp_private_key();
        String alipay_public_key = config.getAlipay_public_key();
        String notify_url = config.getNotify_url();
        String return_url = config.getReturn_url();
        String charset = config.getCharset();
        String sign_type = config.getSign_type();
        String format = config.getFormat();
        String geteway_url = config.getGeteway_url();

        AlipayClient alipayClient = new DefaultAlipayClient(geteway_url, app_id, private_key, format, charset, alipay_public_key, sign_type);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(notify_url);
        request.setReturnUrl(return_url);

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNumber);
        bizContent.put("total_amount", totalAmount);
        bizContent.put("subject", subject);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        request.setBizContent(bizContent.toJSONString());

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
            if (response.isSuccess()) {
                log.info("支付表单生成成功");
                // 生成支付表单HTML
                String form = response.getBody() +
                        "<script>document.forms[0].submit();</script>";

                return form;
            } else {
                log.info("支付表单生成失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean aliSignature(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.certVerifyV1(params, aliPayProperties.getAlipay_public_key(), aliPayProperties.getCharset(), aliPayProperties.getSign_type());
    }

}